package cn.com.egova.permission.google;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import cn.com.egova.permission.R;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * EasyPermissions#hasPermissions(...)  判断
 * EasyPermissions#requestPermissions(...)  申请
 * EasyPermissions.somePermissionPermanentlyDenied(...)  用户第一次选择禁止,第二次进入时弹出对话框.
 *  AfterPermissionGranted:在需要授权后才能执行的方法上添加该注解,会授权成功后自动调用该方法, 不用写监听事件. 当然,这个注解
 *  是可选项,不使用也可以, 但是就需要使用事件监听了,在onPermissionsGranted()里调用需要权限的方法.
 *
 *  java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
 *  缺点:使用google框架的Activity的主题必须是Theme.AppCompat theme. 但是公司大部分项目都是老项目.
 */
public class GoogleActivity extends AppCompatActivity /*implements EasyPermissions.PermissionCallbacks*/{
    private static final String TAG = "GoogleActivity";
    private Button btn_call;
    private String title;
    private static final int RC_CAMERA_AND_CALL_PHONE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google);

        initData();

        initActionBar();

        initView();
    }


    private void initData() {
        title = getIntent().getStringExtra("title");
        if (TextUtils.isEmpty(title)) {
            title = "Permission";
        }
    }

    private void initActionBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle(title);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(false);
        }
    }

    private void initView() {
        btn_call = (Button) findViewById(R.id.btn_call);
        btn_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){  //23,Android 6.0
                    permission();
                }else{
                    call();
                }
            }
        });
    }


    @AfterPermissionGranted(RC_CAMERA_AND_CALL_PHONE)
    private void call() {
        try {
            Intent intent = new Intent(Intent.ACTION_CALL);
            Uri uri = Uri.parse("tel:" + "10086");
            intent.setData(uri);
            startActivity(intent);
            Toast.makeText(this, "打电话啦", Toast.LENGTH_SHORT).show();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }


    private void permission() {
        String[] perms = {Manifest.permission.CAMERA, Manifest.permission.CALL_PHONE};
        if (EasyPermissions.hasPermissions(this, perms)) {
            // Already have permission, do the thing
            call();
        } else {
            // Do not have permissions, request them now
            EasyPermissions.requestPermissions(this, "本功能需要相机和定位的权限",RC_CAMERA_AND_CALL_PHONE, perms);
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
            return true;
        }else{
            return super.onOptionsItemSelected(item);
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this);
    }

    /*@Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        //TODO
        call();
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());

        // (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."
        // This will display a dialog directing them to enable the permission in app settings.
        //下面的2行代码是可选项,为的是防止用户选择永不提醒, 以后再向授权就只能进手动进入设置界面取设置了.
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this).build().show();
        }
    }*/


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
            // Do something after user returned from app settings screen, like showing a Toast.
            Toast.makeText(this, "返回本界面", Toast.LENGTH_SHORT).show();
        }
    }
}
